<template>
  <el-dialog
    :title="editId ? '编辑' : '新增'"
    :visible="showDia"
    width="30%"
    @close="handleClose"
    @open="handleOpen"
  >
    <el-form label-width="80px">
      <el-form-item label="学科名称">
        <el-select
          v-model="formData.subjectID"
          style="width: 80%"
          placeholder="请选择学科"
        >
          <el-option
            v-for="item in subjectList"
            :key="item.id"
            :label="item.subjectName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称">
        <el-input
          v-model="formData.tagName"
          style="width: 80%"
          placeholder="请输入目录名称"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="" size="small" @click="handleClose">取 消</el-button>
      <el-button
        type="primary"
        size="small"
        style="background-color: #409eff; color: #fff"
        @click="handleAdd"
      >
        确 定
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
import {
  getSubjectListAPI,
  addTagAPI,
  getTagDetailAPI,
  putTagAPI
} from '@/api/subject'
export default {
  name: 'DialogView',
  props: {
    showDia: {
      type: Boolean,
      default: false
    },
    editId: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      formData: {
        /*  // subjectID: '',
        directoryName: '' */
      },
      subjectList: []
    }
  },
  methods: {
    // 关闭弹窗
    handleClose() {
      // 传数据
      this.$emit('update:showDia', false)
    },
    // 打开弹窗--选择打开了编辑还是新增
    // 打开弹窗
    async handleOpen() {
      if (this.editId) {
        const resp = await getTagDetailAPI(this.editId)
        // console.log(resp)
        this.formData = resp.data
      }
      const res = await getSubjectListAPI({
        page: 1,
        pagesize: 10000
      })
      console.log(res)
      this.subjectList = res.data.items
    },
    // 打开编辑时使用编辑
    async handleAdd() {
      if (this.editId) {
        await putTagAPI(this.editId, this.formData)
        this.$message.success('修改成功')
        this.handleClose()
      } else {
        // 打开新增时使用新增标签接口
        await addTagAPI(this.formData)
        console.log(this.formData)
        this.handleClose()
        this.$message.success('添加成功')
      }
    }
  }
}
</script>

<style></style>
